<script setup>
import {
  Management,
  Promotion,
  UserFilled,
  User,
  Crop,
  EditPen,
  SwitchButton,
  CaretBottom,
  ChromeFilled,
  Menu
} from '@element-plus/icons-vue'
import avatar from '@/assets/xiaoxiong.png'

import {userInfoService} from '@/api/user.js'
import useUserInfoStore from '@/stores/userInfo.js'
import {useTokenStore} from '@/stores/token.js'

const tokenStore = useTokenStore();
const userInfoStore = useUserInfoStore();
//调用函数,获取用户详细信息
const getUserInfo = async () => {
  //调用接口
  let result = await userInfoService();
  //数据存储到pinia中
  userInfoStore.setInfo(result.data);
}
getUserInfo();
//条目被点击后,调用的函数
import { ref, onMounted, watch } from 'vue';
import {useRouter,useRoute} from 'vue-router'
const router = useRouter();
const route = useRoute();
import {ElMessage, ElMessageBox} from 'element-plus'
const dynamicBreadcrumbs = ref([]);

// 面包屑生成函数
function generateBreadcrumbs() {
  const matched = route.matched;
  return matched.map(record => ({
    path: record.path,
    meta: record.meta // 假设每个路由记录都有一个 meta 属性包含 title
  }));
}

const stateData = ref({
  state: ''
})
// 初始化面包屑
onMounted(() => {
  dynamicBreadcrumbs.value = generateBreadcrumbs();
  stateData.value.state = 3
  stateData.value.state = sessionStorage.getItem("state")
  const value = parseFloat(stateData.value.state);
  console.log(value)
});

// 监听路由变化以更新面包屑
watch(route, () => {
  dynamicBreadcrumbs.value = generateBreadcrumbs();
});

const isAsideCollapsed = ref(false); // 侧边栏初始状态不收缩
// 切换侧边栏状态的方法
const toggleAside = () => {
  isAsideCollapsed.value = !isAsideCollapsed.value;
};

const handleCommand = (command) => {
  //判断指令
  if (command === 'logout') {
    //退出登录
    ElMessageBox.confirm(
        '您确认要退出吗?',
        '温馨提示',
        {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning',
        }
    )
        .then(async () => {
          //退出登录
          //1.清空pinia中存储的token以及个人信息
          tokenStore.removeToken()
          userInfoStore.removeInfo()

          //2.跳转到登录页面
          router.push('/login')
          ElMessage({
            type: 'success',
            message: '退出登录成功',
          })

        })
        .catch(() => {
          ElMessage({
            type: 'info',
            message: '用户取消了退出登录',
          })
        })
  } else {
    //路由
    router.push('/user/' + command)
  }
}
</script>

<template>
  <!-- element-plus中的容器 -->
  <el-container class="layout-container">
    <el-container>
      <!-- 左侧菜单(折叠设置) -->
      <el-aside class="aside" :class="{'aside-collapsed': isAsideCollapsed}"
                :width="isAsideCollapsed ? '70px' : '235px'">
        <!-- element-plus的菜单标签 -->
        <el-menu class="gradient-menu" text-color="#ffffff" background-color="#d5e9ff" router>
          <!-- 网站题目 -->
          <div class="anti-poverty-system" :class="{ 'hide-text': isAsideCollapsed }">
            <span>
              <el-icon color="#ffffff" size="115%" class="icon-adjust">
              <ChromeFilled/>
            </el-icon>
              <span class="menu-text">防返贫监测系统</span></span>
          </div>
          <!--
                    <div class="mt-4">
                      <el-input
                          v-model="input3"
                          style="max-width: 600px"
                          placeholder="查找"
                          class="input-with-select"
                      >
                        <template #prepend>
                          <el-button :icon="Search" />
                        </template>
                      </el-input>
                    </div>
          -->
          <!--          <el-menu-item index="/article/category" :class="{ 'hide-text': isAsideCollapsed }">
                      <el-icon>
                        <Management/>
                      </el-icon>
                      <span class="menu-text">文章分类</span>
                    </el-menu-item>
                    <el-menu-item index="/article/manage" :class="{ 'hide-text': isAsideCollapsed }">
                      <el-icon>
                        <Promotion/>
                      </el-icon>
                      <span  class="menu-text">文章管理</span>
                    </el-menu-item>
                    <el-sub-menu index="1" :class="{ 'hide-text': isAsideCollapsed }">
                      <template #title>
                        <el-icon>
                          <UserFilled/>
                        </el-icon>
                        <span  class="menu-text">个人中心</span>
                      </template>
                      <el-menu-item index="/user/info">
                        <el-icon>
                          <User/>
                        </el-icon>
                        <span>基本资料</span>
                      </el-menu-item>
                      <el-menu-item index="/user/avatar" >
                        <el-icon>
                          <Crop/>
                        </el-icon>
                        <span>更换头像</span>
                      </el-menu-item>
                      <el-menu-item index="/user/resetPassword">
                        <el-icon>
                          <EditPen/>
                        </el-icon>
                        <span>重置密码</span>
                      </el-menu-item>
                    </el-sub-menu>-->
          <el-sub-menu v-if="stateData.state>=5" index="1" :class="{ 'hide-text': isAsideCollapsed }">
            <template #title>
              <el-icon>
                <UserFilled/>
              </el-icon>
              <span class="menu-text">预警统计分析</span>
            </template>
            <el-menu-item index="/stats/overall" :class="{ 'hide-text': isAsideCollapsed }">
              <el-icon>
                <Management/>
              </el-icon>
              <span>总体情况分析</span>
            </el-menu-item>
            <el-menu-item index="/stats/clue">
              <el-icon>
                <Management/>
              </el-icon>
              <span>线索类型分析</span>
            </el-menu-item>
            <el-menu-item index="/stats/handling">
              <el-icon>
                <Management/>
              </el-icon>
              <span>处理情况分析</span>
            </el-menu-item>
          </el-sub-menu>

          <el-sub-menu v-if="stateData.state==1||stateData.state==6" index="2"
                       :class="{ 'hide-text': isAsideCollapsed }">
            <template #title>
              <el-icon>
                <UserFilled/>
              </el-icon>
              <span class="menu-text">风险来源</span>
            </template>
            <el-menu-item index="/farmer/applypoor">
              <el-icon>
                <Management/>
              </el-icon>
              <span>农户主动申请</span>
            </el-menu-item>
            <el-menu-item index="/verify/department">
              <el-icon>
                <Management/>
              </el-icon>
              <span>部门信息对比</span>
            </el-menu-item>
          </el-sub-menu>

          <el-sub-menu v-if="stateData.state==2||stateData.state==6" index="3"
                       :class="{ 'hide-text': isAsideCollapsed }">
            <template #title>
              <el-icon>
                <UserFilled/>
              </el-icon>
              <span class="menu-text">核实处理</span>
            </template>
            <el-menu-item index="/approval/back">
              <el-icon>
                <Management/>
              </el-icon>
              <span>打回核实处理</span>
            </el-menu-item>
            <el-menu-item index="/approval/village">
              <el-icon>
                <Management/>
              </el-icon>
              <span>村级核实处理</span>
            </el-menu-item>
            <el-menu-item index="/approval/town">
              <el-icon>
                <Management/>
              </el-icon>
              <span>乡级核实处理</span>
            </el-menu-item>
            <el-menu-item index="/approval/county">
              <el-icon>
                <Management/>
              </el-icon>
              <span>县级核实处理</span>
            </el-menu-item>
            <el-menu-item index="/approval/city">
              <el-icon>
                <Management/>
              </el-icon>
              <span>市级核实处理</span>
            </el-menu-item>
            <el-menu-item index="/approval/province">
              <el-icon>
                <Management/>
              </el-icon>
              <span>省级核实处理</span>
            </el-menu-item>
          </el-sub-menu>

          <el-menu-item v-if="stateData.state==3||stateData.state==6" index="/monitor/IntensiveMonitor"
                        :class="{ 'hide-text': isAsideCollapsed }">
            <el-icon>
              <Management/>
            </el-icon>
            <span class="menu-text">监测管理</span>
          </el-menu-item>
          <el-sub-menu index="6" :class="{ 'hide-text': isAsideCollapsed }">
            <template #title>
              <el-icon>
                <UserFilled/>
              </el-icon>
              <span class="menu-text">帮扶安排</span>
            </template>
            <el-menu-item index="/farmer/Help">
              <el-icon>
                <Management/>
              </el-icon>
              <span>帮扶安排</span>
            </el-menu-item>
          </el-sub-menu>

          <el-sub-menu v-if="stateData.state==4||stateData.state==6" index="6"
                       :class="{ 'hide-text': isAsideCollapsed }">
            <template #title>
              <el-icon>
                <UserFilled/>
              </el-icon>
              <span class="menu-text">用户管理</span>
            </template>
            <el-menu-item index="/UserManage/farmer">
              <el-icon>
                <Management/>
              </el-icon>
              <span>农户信息管理</span>
            </el-menu-item>
            <el-menu-item index="/UserManage/leader">
              <el-icon>
                <Management/>
              </el-icon>
              <span>干部信息管理</span>
            </el-menu-item>
            <el-menu-item index="/UserManage/helpman">
              <el-icon>
                <Management/>
              </el-icon>
              <span>帮扶人信息管理</span>
            </el-menu-item>
          </el-sub-menu>
          <el-sub-menu v-if="stateData.state==5||stateData.state==6" index="7"
                       :class="{ 'hide-text': isAsideCollapsed }">
            <template #title>
              <el-icon>
                <UserFilled/>
              </el-icon>
              <span class="menu-text">系统管理</span>
            </template>
            <el-menu-item index="/SystemManage/RiskManage">
              <el-icon>
                <Management/>
              </el-icon>
              <span>风险点管理</span>
            </el-menu-item>
            <el-menu-item index="/SystemManage/HelpManage">
              <el-icon>
                <Management/>
              </el-icon>
              <span>帮扶措施管理</span>
            </el-menu-item>
          </el-sub-menu>

        </el-menu>
      </el-aside>
      <!-- 右侧主区域 -->
      <el-container>
        <!-- 头部区域 -->
        <el-header>
          <!--面包屑设计-->
          <div class="breadcrumb-wrapper">
            <div class="breadcrumb-nickname">
              <el-button class="el-button" type="primary" :icon="Menu" plain @click="toggleAside">
                {{ isAsideCollapsed ? '展开' : '收缩' }}
              </el-button>
              <strong>{{ userInfoStore.info.nickname }}</strong>
            </div>
            <el-breadcrumb separator="/" class="el-bread" v-if="dynamicBreadcrumbs.length>1">
              <!-- 动态生成当前页面路径的面包屑 -->
              <el-breadcrumb-item v-for="(route, index) in dynamicBreadcrumbs" :key="index">
                <router-link :to="route.path">
                  {{ route.meta.title }}
                </router-link>
              </el-breadcrumb-item>
            </el-breadcrumb>
          </div>
          <!--          <div><strong>{{ userInfoStore.info.nickname }}</strong></div>
                          <div>黑马程序员：<strong>{{ userInfoStore.info.nickname }}</strong></div>
                     下拉菜单
                     command: 条目被点击后会触发,在事件函数上可以声明一个参数,接收条目对应的指令 -->
          <el-dropdown placement="bottom-end" @command="handleCommand">
                    <span class="el-dropdown__box">
                        <el-avatar :src="userInfoStore.info.userPic? userInfoStore.info.userPic:avatar"/>
                        <el-icon>
                            <CaretBottom/>
                        </el-icon>
                    </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="info" :icon="User">基本资料</el-dropdown-item>
                <el-dropdown-item command="avatar" :icon="Crop">更换头像</el-dropdown-item>
                <el-dropdown-item command="resetPassword" :icon="EditPen">重置密码</el-dropdown-item>
                <el-dropdown-item command="logout" :icon="SwitchButton">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </el-header>
        <!-- 中间区域 -->
        <el-main>
          <!-- <div style="width: 1290px; height: 570px;border: 1px solid red;">
              内容展示区
          </div> -->
          <router-view></router-view>
        </el-main>
        <!-- 底部区域 -->
        <el-footer>防返贫监测与帮扶管理系统 ©2024 Created by 不好好学习没饭吃6人组</el-footer>
      </el-container>
    </el-container>
  </el-container>
</template>

<style lang="scss" scoped>
.layout-container {
  height: 100vh;

  .el-aside {
    border-bottom-right-radius: 10px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.4);

    &__logo {
      height: 120px;
      //background: url('@/assets/logo.png') no-repeat center / 120px auto;
    }

    .el-menu {
      border-right: none;
    }
  }

  .el-header {
    background-color: #ffffff;
    border: 1px solid #ddd; /* 灰色边框 */
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); /* 阴影*/
    color: #000000;
    height: 85px;
    padding: 30px;
    /*    background-color: #426289;*/
    border-left: 1px solid rgba(0, 0, 0, 0.2);
    display: flex;
    align-items: center;
    justify-content: space-between;
    box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.4);

    .el-dropdown__box {
      display: flex;
      align-items: center;

      &:active,
      &:focus {
        outline: none;
      }
    }
  }

  .el-footer {
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    color: #666;
  }

  .anti-poverty-system {
    padding: 20px;
    color: #ffffff;
    font-family: 'Arial', sans-serif;
    font-size: 20px;
    font-weight: bold;
  }

  .gradient-menu {
    height: 100%;
    background: linear-gradient(135deg, #6692d9, #7ba9e6, #9cd5f9) !important;
    background: linear-gradient(#83ace7, #9cd5f9) !important;
    //background: linear-gradient(#292a3a, #536976)!important;
    //background: linear-gradient(135deg, #272727, #4a4a4a, #6d6d6d, #909090)!important;
    //background: linear-gradient(#242541, #46607d)!important;
    /* 其他可能的样式覆盖 */
  }

  .icon-adjust {
    padding-top: 10px;
    margin-right: 10px;
  }

  .el-breadcrumb__inner a, .el-breadcrumb__inner.is-link {
    font-weight: 600;
    font-size: 104%;
    color: #888888;
    letter-spacing: 0.05em;
  }

  .el-menu el-menu--inline {
    el-menu-bg-color: #888888;
  }

  .bread-container {
    display: flex;
    justify-content: flex-start; /* 使子元素靠左对齐 */
  }

  .breadcrumb-wrapper {
    display: flex;
    align-items: center; /* 垂直居中对齐子元素 */
  }

  .breadcrumb-nickname {
    font-size: 105%;
    display: flex;
    align-items: center; /* 垂直居中对齐昵称和按钮 *//* 根据需要调整昵称和面包屑之间的间距 */
  }

  .el-button {
    margin-right: 20px;
  }

  .aside-collapsed {
    overflow: hidden;
    /*    transition: width 0.2s ease;*/
  }

  .hide-text .menu-text {
    display: none;
  }

  /*    .el-menu-item:hover {
        background-color:  #1E90FF !important;
        color: #ffffff;}*/
  .el-menu-item:hover {
    background-color: transparent !important; /* 使用 !important 可以确保这个样式被优先应用 */
  }

  /*
  .el-sub-menu:hover {
    background-color: #1E90FF !important;
  }
  */


  /*  .mt-4{
      padding:15px;
    }*/
}
</style>